package com.kabuda.hot100.二叉树.easy;

import com.kabuda.hot100.二叉树.TreeNode;

/**
 * @author kabuda
 * @date 2025-03-08 17:18
 * @description 给你一棵二叉树的根节点 root ，翻转这棵二叉树，并返回其根节点。
 */
public class t226_翻转二叉树 {
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(3);
        TreeNode treeNode2 = new TreeNode(9);
        TreeNode treeNode3 = new TreeNode(20);
        TreeNode treeNode4 = new TreeNode(15);
        TreeNode treeNode5 = new TreeNode(7);
        treeNode1.left = treeNode2;
        treeNode1.right = treeNode3;
        treeNode3.left = treeNode4;
        treeNode3.right = treeNode5;

        System.out.println(treeNode1);
        System.out.println(invertTree(treeNode1));
    }

    public static TreeNode invertTree(TreeNode root) {
        recursion(root);
        return root;
    }


    public static void recursion(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left != null || root.right != null) {
            TreeNode temp = root.left;
            root.left = root.right;
            root.right = temp;
        }

        if (root.left != null) {
            recursion(root.left);
        }

        if (root.right != null) {
            recursion(root.right);
        }
    }


}
